package com.sq.partner.manager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.ActirationErrorInfo;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.dao.ActivationCardSheetDao;
import com.sq.partner.manager.mybatis.model.ActirationCardMinMaxDto;
import com.sq.partner.manager.mybatis.model.ActirationCardReceiveDto;
import com.sq.partner.manager.mybatis.model.ActirationCardResultDto;
import com.sq.partner.manager.mybatis.model.ActivationCardSheet;
import com.sq.partner.manager.mybatis.model.DeptDto;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.ActirationCardCirculationService;
import com.sq.partner.manager.util.ListSplitUtil;
import com.sq.partner.manager.util.Page;

@Service
public class ActirationCardCirculationServiceImpl implements ActirationCardCirculationService{
	
	@Resource
	private ActivationCardSheetDao cardSheetDao;
	@Override
	public Page<String> queryCardNum(Page<String> page) throws Exception{
		return cardSheetDao.getLzNoPage(page);
	}
	@Override
	public ActirationCardMinMaxDto queryMinMax(Map<String,Object> map) throws Exception{
		return cardSheetDao.getMinMax(map);
	}
	@Override
	public Page<ActirationCardMinMaxDto> queryAllMinMax(Page<ActirationCardMinMaxDto> page) throws Exception{
		return cardSheetDao.getMinMaxPage(page);
	}
	@Override
	public ActirationCardResultDto circulation(GatewayUser user,String cardId,String startNo,String number,String toDep,String flag) throws Exception{
		ActirationCardResultDto resultDto=new ActirationCardResultDto();
		resultDto.setSuccess(false);
		//flag翻译  1上交0下发
		if("1".equals(flag)){
			flag=Constants.ACTIR_CARD_HAND_UP;
		}else if("0".equals(flag)){
			flag=Constants.ACTIR_CARD_HAND_DOWN;
		}else{
			flag="";
		}
		StringBuffer message=new StringBuffer();
//		StringBuffer runerror=new StringBuffer("激活卡操作失败:");
		//放入条件
		HashMap<String,Object> paramMap=new HashMap<String, Object>();
		paramMap.put("cardDefineId", cardId);
		paramMap.put("startNo", startNo);
		paramMap.put("depNo", user.getUserComcode());
		paramMap.put("number", number);
		//校验临时用的状态
		paramMap.put("status", Constants.ACTIR_CARD_STOCK);
		paramMap.put("tostatus", flag);
		paramMap.put("receiveDepNo", toDep);
		paramMap.put("operateuser", user.getUserCode());
		//查询所操作的卡号集合
		Integer noListCount=cardSheetDao.getMaxNum(paramMap);
		//校验下发数量，如果下发数量不一致则提示
		if(noListCount.intValue()<Integer.valueOf(number).intValue()){
			message.append("库存流转数量不足;需流转:"+number+";实际可流转库存:"+noListCount+";");
		}else{
			//流转数据操作
			if(cardSheetDao.updateDoCircul(paramMap)){
				resultDto.setSuccess(true);
			}
		}
		//循环执行流转操作
//		for(String no:noList){
//			ActivationCardSheet sheet=new ActivationCardSheet();
//			sheet.setCardNo(no);
//			sheet.setCardDefineId(Integer.valueOf(cardId));
//			sheet.setStatus(flag);
//			sheet.setReceiveDepNo(toDep);
//			if(!cardSheetDao.update(sheet)){
//				resultDto.setSuccess(false);
//				runerror.append(no+";");
//			}
//		}
		
		//判断返回
		if(!resultDto.isSuccess()){
			resultDto.setMessage(message.toString());
		}
		return resultDto;
	}
	@Override
	public ActirationCardResultDto receiveCard(GatewayUser user,String cardId,List<String> nOList) throws Exception{
		ActirationCardResultDto resultDto=new ActirationCardResultDto();
		resultDto.setSuccess(false);
//		StringBuffer runerror=new StringBuffer("以下激活卡号接收失败:");
//		for(String no:nOList){
//			ActivationCardSheet sheet=new ActivationCardSheet();
//			sheet.setCardDefineId(Integer.valueOf(cardId));
//			sheet.setCardNo(no);
//			String userdep=user.getUserComcode();
//			sheet.setDeptno(userdep);
//			sheet.setStatus(Constants.ACTIR_CARD_STOCK);
//			sheet.setReceiveDepNo("");
//			//如果不成功
//			if(!cardSheetDao.update(sheet)){
//				resultDto.setSuccess(false);
//				runerror.append(no+";");
//			}
//		}
		//分割集合		
		List<List<String>> nOListSub = ListSplitUtil.splitListBySizeBatch(nOList, 1000, 5);
		HashMap<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("nolist", nOListSub);
		paramMap.put("receiveno", user.getUserComcode());
		paramMap.put("deptno", user.getUserComcode());
		paramMap.put("operateuser", user.getUserCode());
		paramMap.put("status", Constants.ACTIR_CARD_STOCK);
		
		//更新
		if(cardSheetDao.updateDoReceive(paramMap)){
			resultDto.setSuccess(true);
		}else{
			resultDto.setMessage(ActirationErrorInfo.CIRCUL_11);
		}
		
		return resultDto;
	}
	@Override
	public Page<ActirationCardReceiveDto> queryReceive(Page<ActirationCardReceiveDto> page) throws Exception{
		return cardSheetDao.getReceivePage(page);
	}
	@Override
	public Page<String> queryReceiveList(Page<String> page) throws Exception{
		return cardSheetDao.getReceiveNoList(page);
	}
	@Override
	public List<DeptDto> queryComDown(String userCom) throws Exception{
		return cardSheetDao.getComDown(userCom);
	}
	@Override
	public List<DeptDto> queryComUp(String userCom) throws Exception{
		return cardSheetDao.getComUp(userCom);
	}
	@Override
	public Page<ActivationCardSheet> queryStock(Page<ActivationCardSheet> page) throws Exception{
		return cardSheetDao.queryPage(page);
	}
	@Override
	public String queryMaxnum(HashMap<String, Object> paramMap) throws Exception{
		Integer num=cardSheetDao.getMaxNum(paramMap);
		return num+"";
	}
	@Override
	public String queryjsMaxnum(HashMap<String, Object> paramMap) throws Exception{
		Integer num=cardSheetDao.getjsMaxNum(paramMap);
		return num+"";
	}
	@Override
	public ActirationCardResultDto cardDelayList(GatewayUser user,List<String> delayList,String date) throws Exception{
		ActirationCardResultDto resultDto=new ActirationCardResultDto();
		resultDto.setSuccess(true);
		//错误信息
		StringBuffer error=new StringBuffer("以下激活卡操作失败：");
		//查询条件
		HashMap<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("time", date);
		paramMap.put("status", delayStatus());
		//循环执行
		for(String cardNo:delayList){
			paramMap.put("cardNo", cardNo);
			//如果失败
			if(!cardSheetDao.delayOne(paramMap)){
				resultDto.setSuccess(false);
				error.append(cardNo+",");
			}
		}
		resultDto.setMessage(error.toString());
		return resultDto;
	}
	@Override
	public ActirationCardResultDto cardDelaybyNoNum(GatewayUser user,String startNo,String endNo,String date) throws Exception{
		ActirationCardResultDto resultDto=new ActirationCardResultDto();
		resultDto.setSuccess(false);
		long num=Long.valueOf(endNo)-Long.valueOf(startNo)+1;
		HashMap<String, Object> paramMap=new HashMap<String, Object>();
		//放入查询条件
		paramMap.put("status", delayStatus());
		paramMap.put("startNo", startNo);
		paramMap.put("endNo", endNo);
		paramMap.put("time", date);
		Integer count=cardSheetDao.queryDelayCount(paramMap);
		//校验张数是否一致
		if(count!=num){
			resultDto.setMessage(ActirationErrorInfo.CIRCUL_8);
		}else{
			boolean flag=cardSheetDao.delayByStartAndEnd(paramMap);
			resultDto.setSuccess(flag);
		}
		return resultDto;
	}
	/**
	 * 拼接可延期状态
	 * @return
	 */
	private String delayStatus(){
		//拼接状态
		String status="'"+Constants.ACTIR_CARD_STOCK+"',"+
					  "'"+Constants.ACTIR_CARD_HAND_UP+"',"+
					  "'"+Constants.ACTIR_CARD_HAND_DOWN+"',"+
					  "'"+Constants.ACTIR_CARD_RELATION+"',"+
					  "'"+Constants.ACTIR_CARD_OVERDUE+"'";
		return status;
	}
	/**
	 * 查询本级机构
	 */
	@Override
	public List<DeptDto> queryComAndDown(String userCom) throws Exception{
		return cardSheetDao.getComAndDown(userCom);
	}
	@Override
	public boolean updateStatusForTimerTask(HashMap<String,Object> map)
			throws Exception {
		return cardSheetDao.updateStatusForTimerTask(map);
	}
}
